<?php
error_reporting(0);

include 'fb/facebook-php-sdk-2343fca/src/facebook.php';


class FB{
	public $token;
	
	//public $APP_ID = '257478380945147';
	//public $APP_SECREAT = '5d55c36e1ce492427ea9b870c6b6cd76';
	
	public function __construct( $registry, $directCall=true )
	{
		$this->registry = $registry;
	}

	public function fbconnect(){
		 
		$facebook = new Facebook(array(
			'appId' => '257478380945147',
			'secret' => '5d55c36e1ce492427ea9b870c6b6cd76',
			'cookie' => true,
			'code' => $code,
		));

		$app_id = '257478380945147';
		$app_secret = "5d55c36e1ce492427ea9b870c6b6cd76";
	  
		$my_url = "http://duminda.net76.net/fb/home/";
		//$my_url = "http://duminda.net76.net/profile/home/1/5/facebook";
		$code = $_REQUEST["code"];

		if(empty($code)) {
			$dialog_url = "http://www.facebook.com/dialog/oauth?client_id="
			. $app_id . "&redirect_uri=" . urlencode($my_url)."&scope=email,read_stream,publish_stream";

			echo("<script> top.location.href='" . $dialog_url . "'</script>");
		}

		$token_url = "https://graph.facebook.com/oauth/access_token?client_id="
		. $app_id . "&redirect_uri=" . urlencode($my_url) . "&client_secret="
		. $app_secret . "&code=" . $code;

		$access_token = file_get_contents($token_url);
		$graph_url = "https://graph.facebook.com/me?" . $access_token;
		$user = json_decode(file_get_contents($graph_url));

		print_r($user->id);
		global $response; 
		 
	}


	public function fb_home(){
			
		$this->registry->getObject('template')->buildFromTemplates( 'main/main.tpl.php', 'main/main_menu.tpl.php', 'main/main_content.tpl.php' ,'profile/facebook/information/view.tpl.php', 'main/footer.tpl.php' );

		$facebook = new Facebook(array(
			'appId' => '257478380945147',
			'secret' => '5d55c36e1ce492427ea9b870c6b6cd76',
			'cookie' => true,
			'code' => $code,
		));
		//get user id and profile type of the profile
		// and add to redirect URL
		$user_id = "";
		$profile_type = "";
		if(isset($_GET['user_id']) ){
			 
			$user_id = $_GET['user_id'];
		}
		if(isset($_GET['profile_id']) ){
			 
			$profile_type = $_GET['profile_id'];
		}

		 
		$app_id = '257478380945147';
		$app_secret = "5d55c36e1ce492427ea9b870c6b6cd76";
		$my_url = "http://duminda.net76.net/fb/home/";

		$code = $_REQUEST["code"];
		//echo $code;
		if(empty($code)) {
			$dialog_url = "http://www.facebook.com/dialog/oauth?client_id="
			. $app_id . "&redirect_uri=" . urlencode($my_url)."&scope=email,read_stream,publish_stream";

			echo("<script> top.location.href='" . $dialog_url . "'</script>");
		}

		$token_url = "https://graph.facebook.com/oauth/access_token?client_id="
		. $app_id . "&redirect_uri=" . urlencode($my_url) . "&client_secret="
		. $app_secret . "&code=" . $code;

		$access_token = file_get_contents($token_url);
		$graph_url = "https://graph.facebook.com/me?" . $access_token;
		$user = json_decode(file_get_contents($graph_url));

		$_SESSION['user'] = $user->id;
		 
		$_SESSION['token'] = $access_token;
			
		echo $access_token."<br/>";
		$query = "https://api.facebook.com/method/fql.query?query=";
		$query .= urlencode("SELECT post_id, actor_id, target_id, message FROM stream WHERE filter_key = 'others' AND source_id = me()");

		$query .= "&".$access_token."&format=json";
		$out = file_get_contents( $query);

		$response = json_decode($out);

		print_r($response);
			
	}

	function fb_profile(){
		 
		$this->registry->getObject('template')->buildFromTemplates( 'main/main.tpl.php', 'main/main_menu.tpl.php', 'main/main_content.tpl.php' ,'profile/facebook/information/view.tpl.php', 'main/footer.tpl.php' );
			
		$graph_url = "https://graph.facebook.com/me?" . $_SESSION['token'];
		$user = json_decode(file_get_contents($graph_url));

		print_r($user)."<br/>";



	}


	function fb_status_update(){
			
		$this->registry->getObject('template')->buildFromTemplates( 'main/main.tpl.php', 'main/main_menu.tpl.php', 'main/main_content.tpl.php' ,'profile/facebook/information/view.tpl.php', 'main/footer.tpl.php' );
			
		$facebook = new Facebook(array(
		'appId' => '257478380945147',
		'secret' => '5d55c36e1ce492427ea9b870c6b6cd76',
		'cookie' => true,
		'code' => $code,
		));
			
		echo $_SESSION['user'];
			
		$status = 'test status5';
		$statusUpdate = $facebook->api('/'.$_SESSION['user'].'/feed', 'post', array('message'=> $status, 'cb' => ''));
			
		print_r($statusUpdate) ;
	}


}
?>